<?php
namespace app\home\controller;

use app\home\model\VoteInfo;
use app\home\model\VoteOption;
use app\home\model\VoteRecord;
use think\Controller;

class Index extends Controller
{
    /**
     * 返回客户端的IP地址
     * @param boolean $isInt 所否返回整形IP地址
     * @return mixed $ip 用户IP地址
     */
    public static function getClientIp($isInt = false)
    {
        //$ip = false;
        if(!empty($_SERVER['REMOTE_ADDR'])) {
            $ip = $_SERVER["REMOTE_ADDR"];
        } else if(! empty($_SERVER["HTTP_CLIENT_IP"])){
            $ip = $_SERVER["HTTP_CLIENT_IP"];
        }

        if(! empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
            $ips = explode(", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
            if($ip){
                array_unshift($ips, $ip);
                $ip = FALSE;
            }
            for($i = 0; $i < count($ips); $i ++){
                if(! preg_match("/^(0|10|100|127|172\.16|192\.168)\./", $ips[$i])){
                    $ip = $ips[$i];
                    break;
                }
            }
        }
        $ip = $ip ? $ip : $_SERVER['REMOTE_ADDR'];
        $ip = $ip ? $ip : '0.0.0.0';

        return $isInt ? self::ip2long($ip) : $ip;
    }

    public static function ip2long($ip)
    {
        return bindec(decbin(ip2long($ip)));
    }

    public function index()
    {
        $data['current'] = 'vote';
        $vote_list = VoteInfo::all(['delete_time' => 0]);
        $data['vote_list'] = $vote_list;
        $this->view->engine->layout(false);
        return view('index', $data);
    }

    public function view()
    {
        $id = input('id');
        if (!$id) {
            $this->error('投票不存在', 'Index/index');
        }
        $info = VoteInfo::get($id);
        if (!$info) {
            $this->error('投票不存在', 'Index/index');
        }
        $this->view->engine->layout(false);
        //查询投票选项
        $optionData = VoteOption::all(['vote_id' => $id]);

        $ip = self::getClientIp(true);
        //查询是否已经投过票
        $hasInfo = VoteRecord::get(['ip'    => $ip, 'vote_id'   => $id]);

        return view('view', [
            'vote_info' => $info,
            'option_list' => $optionData ? $optionData : [],
            'hasInfo'   => $hasInfo
        ]);

    }

    public function choose()
    {
        $vote_id = input('vote_id');
        $option_id = input('option_id');
        $ip = self::getClientIp(true);
        //查询是否已经投过票
        $hasInfo = VoteRecord::get(['ip'    => $ip, 'vote_id'   => $vote_id]);
        if ($hasInfo) {
            exit(json_encode(['status'=> 1001, 'info' => '你已经参与过']));
        }
        $info = new VoteRecord();
        if ($info->save([
            'vote_id'   => $vote_id,
            'vote_option_id' => $option_id,
            'ip'    => $ip,
            'create_time'   => time()
        ])) {
            $voteInfo = VoteInfo::get($vote_id);
            $voteInfo->save([
                'voter' => $voteInfo->voter + 1
            ]);
            $optionInfo = VoteOption::get($option_id);
            $optionInfo->save([
                'voters' => $optionInfo->voters + 1
            ]);
            exit(json_encode(['status'=> 200, 'info' => '你已经参与过']));
        }
        exit(json_encode(['status'=> 1001, 'info' => '投票失败']));

    }
}
